package com.sap.model
{
	import flash.net.getClassByAlias;
	
	import mx.collections.ArrayCollection;

	[Bindable]
	public class DataModel
	{
		private static var _instance:DataModel;
		
		public var saleData:ArrayCollection = new ArrayCollection;
		public var selectedYear:String;
		public var rawData:Object = new Object;
		public var countryList:ArrayCollection = new ArrayCollection;
		public var uniqueQuarters:ArrayCollection = new ArrayCollection(['Q-1','Q-2','Q-3','Q-4']);
		public var firstQuarterMonths:Array = ['Jan','Feb','Mar'];
		public var secondQuarterMonths:Array = ['Apr','May','Jun'];
		public var thirdQuarterMonths:Array = ['Jul','Aug','sep'];
		public var fourthQuarterMonths:Array = ['Oct','Nov','Dec'];
		
		public static function getInstance():DataModel{
			if(_instance != null)
				return _instance;
			else
				return _instance = new DataModel;
		}
		
		public function getUniqueValues(node:Object, value:String, uniqueValues:ArrayCollection):ArrayCollection
		{  
			var found:Boolean;
			if(uniqueValues.length == 0){
				uniqueValues.addItem(node[value]);
				return uniqueValues;					
			}
			for(var i:int = 0; i < uniqueValues.length; i++){
				if(uniqueValues[i] == node[value]){
					found = true;
					break;
				}
			}
			if(!found)
				uniqueValues.addItem(node[value]);
			
			return uniqueValues;  
		}
		
		public var yearConsolidate:ArrayCollection;
		public var countriesConsolidate:ArrayCollection;
		public var quartersConsolidate:ArrayCollection;
		public var monthConsolidate:ArrayCollection;
		public var uniqueProducts:ArrayCollection = new ArrayCollection;
		public var uniqueCountries:ArrayCollection = new ArrayCollection;
		
		public var consolidatedProducts:ArrayCollection = new ArrayCollection;
		public var consolidatedCountries:ArrayCollection = new ArrayCollection;
		public var consolidatedQuarters:ArrayCollection = new ArrayCollection;
		public var consolidatedMonths:ArrayCollection = new ArrayCollection;
		
		public var selectedState:String = "";
		
		public function getDrilledProductConsolidate(year:String):void{
			yearConsolidate = new ArrayCollection(saleData.source);
			
			yearConsolidate.filterFunction = filterYear;
			yearConsolidate.refresh();
			
			for (var i:int = 0; i < yearConsolidate.length; i++)
				uniqueProducts = getUniqueValues(yearConsolidate[i], 'productDesc', uniqueProducts);
			
			var tempTotal:Number = 0;
			for(var j:int = 0; j < uniqueProducts.length; j++){
				for(var k:int = 0; k < yearConsolidate.length; k++){
					if(uniqueProducts[j] == yearConsolidate[k]['productDesc']){
						tempTotal += Number(yearConsolidate[k]['sales']);
					}
				}
				var obj:Object = new Object;
					obj.product = uniqueProducts[j];
					obj.total = tempTotal;
				consolidatedProducts.addItem(obj);
				tempTotal = 0;
			}
		}
		
		public var selectedProduct:String;
		public function getDrilledCountryData(value:String):void{
			if(value !="")
				selectedProduct = value;
			countriesConsolidate = new ArrayCollection(saleData.source);
			countriesConsolidate.filterFunction = filterCountries;
			countriesConsolidate.refresh();
			
			for(var i:int =0;  i < countriesConsolidate.length; i++){
				getUniqueValues(countriesConsolidate[i], 'countryDesc',uniqueCountries); 
			}
			
			var tempTotal:Number = 0;
			for(var j:int =0; j< uniqueCountries.length; j++){
				for(var k:int = 0; k < countriesConsolidate.length; k++){
					if(selectedProduct == countriesConsolidate[k]['productDesc']){
						tempTotal += Number(countriesConsolidate[k]['sales']);
					}
				}
				var obj:Object = new Object;
					obj.country = uniqueCountries[j];
					obj.total = tempTotal;
				consolidatedCountries.addItem(obj);
			}
		}
		
		public var selectedCountry:String;
		
		public function getDrilledQuarterData(value:String):void{
			if(value !="")
				selectedCountry = value;
			quartersConsolidate = new ArrayCollection(saleData.source);
			quartersConsolidate.filterFunction = filterQuarters;
			quartersConsolidate.refresh();
			
			var tempTotal:Number = 0;
			
			for(var j:int =0; j< uniqueQuarters.length; j++){
				for(var k:int = 0; k < quartersConsolidate.length; k++){
					if(uniqueQuarters[j] == quartersConsolidate[k]['quarter']){
						tempTotal += Number(quartersConsolidate[k]['sales']);
					}
				}
				var obj:Object = new Object;
				obj.quarter = uniqueQuarters[j];
				obj.total = tempTotal;
				tempTotal = 0;
				consolidatedQuarters.addItem(obj);
			}
		}
		
		public var selectedQuarter:String;
		public function getDrilledMonthData(value:String):void{
			if(value !="")
				selectedQuarter = value;
			monthConsolidate = new ArrayCollection(saleData.source);
			monthConsolidate.filterFunction = filterMonths;
			monthConsolidate.refresh();
			
			var tempTotal:Number = 0;
			var monthList:Array = getMonthsList(selectedQuarter);
			for(var j:int =0; j< monthList.length; j++){
				for(var k:int = 0; k < monthConsolidate.length; k++){
					if(monthList[j] == monthConsolidate[k]['month']){
						tempTotal += Number(monthConsolidate[k]['sales']);
					}
				}
				var obj:Object = new Object;
				obj.month = monthList[j];
				obj.total = tempTotal;
				tempTotal = 0;
				consolidatedMonths.addItem(obj);
			}
		}
		
		private function getMonthsList(value:String):Array{
			if(value == 'Q-1'){
				return firstQuarterMonths;
			}
			else if(value == 'Q-2'){
				return secondQuarterMonths;
			}
			else if(value == 'Q-3'){
				return thirdQuarterMonths;
			}
			else if(value == 'Q-4'){
				return fourthQuarterMonths;
			}
			return [];
		}
		
		public function refreshData():void{
			uniqueProducts = new ArrayCollection;
			uniqueCountries = new ArrayCollection;
			consolidatedProducts = new ArrayCollection;
			
			getDrilledProductConsolidate(selectedYear);
			
			consolidatedCountries = new ArrayCollection;
			if(selectedProduct)
				getDrilledCountryData(selectedProduct);
			
			consolidatedQuarters = new ArrayCollection;
			if(selectedCountry)
				getDrilledQuarterData(selectedCountry);
			
			consolidatedMonths = new ArrayCollection;
			if(selectedQuarter)
				getDrilledMonthData(selectedQuarter);
		}
		
		private function filterMonths(item:Object):Boolean{
			return item.year == selectedYear && item.productDesc == selectedProduct && item.countryDesc == selectedCountry && item.quarter == selectedQuarter;
		}
		
		private function filterQuarters(item:Object):Boolean{
			return item.year == selectedYear && item.productDesc == selectedProduct && item.countryDesc == selectedCountry;
		}
		
		private function filterCountries(item:Object):Boolean{
			return item.year == selectedYear && item.productDesc == selectedProduct;
		}
		
		private function filterYear(item:Object):Boolean{
			return item.year == selectedYear;
		}
		
		public function clearCountry():void{
			countriesConsolidate = new ArrayCollection;
			consolidatedCountries = new ArrayCollection;
		}
		
		public function clearQuarter():void{
			quartersConsolidate =  new ArrayCollection;
			consolidatedQuarters = new ArrayCollection;
		}
		public function clearMonth():void{
			monthConsolidate = new ArrayCollection;
			consolidatedMonths = new ArrayCollection;
		}
	}
}